<template>
	<view class="history" v-if="history.total">
		<view class="header">
			<view class="title">历史回看</view>
			<view class="more" @click="goToMoreHistory()">更多</view>
		</view>
		
		<view class="list">
			<view class="item" v-for="(item,index) in history.data" :key="index" @click="goToMoreHistory()">
				<view class="content">
					<text class="start-time">{{item.start_time_str}}</text>
					<text class="desc"> 直播回看</text>
				</view>
			</view>
		</view>
		
		<!--登录框-->
		<Login ref="login" :config="config" @getUserInfo="getUserInfo"/>
	</view>
</template>

<script>
	export default {
		name: 'LiveHistory',
		props:{
			config: {
				type: Object,
				default: {}
			},
			roomId: {
				type: Number,
				default: ''
			},
			liveData: {
				type: Object,
				default: {}
			},
			
		},
		data() {
			return {
				userInfo: {},
				history: {},
			}
		},
		created() {
			//获取已登录用户数据
			this.getUserInfo()
			this.getHistory()
		},
		methods: {
			/**
			 * 获取回看列表2条
			 */
			getHistory: function(){
				let that = this
				let params = this.urlParams
				let data = {}
					data.roomid = this.roomId
					data.rows = 2
				//获取列表数据
				let url = '';
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/livecourse'
					data.action = 'history'
					data.m = 'muu_classroom'
					//#endif
					//#ifdef H5
					url = 'entry/site/history'
					data.action = 'lists'
					data.m = 'muu_classroom_plugin_livecourse'
					//#endif
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							that.history = res.data.data
						}
					}
				})
			},
			/**
			 * 路由至更多直播回看列表
			 * @param {Object} type
			 * @param {Object} type_id
			 */
			goToMoreHistory: function(){
				console.log(this.userInfo.memberInfo)
				if (this.userInfo.memberInfo == undefined) {
					//触发登录框
					this.$refs.login.loginDialog()
					return false
				}else if(this.liveData.purchase == 0){
					//触发提示框
					this.showPayModal()
					return false
				}else{
					let roomid = this.roomId
					let urlParams = 'roomid='+roomid
					
					let url = '/muu_classroom/pages/live/history?' + urlParams
					
					//指定跳转地址
					uni.navigateTo({
					    url: url
					});
				}
			},
			
			/**
			 * 显示购买提示模态框
			 */
			showPayModal: function(){
				uni.showModal({
					title: '温馨提示',
					content: '订阅后观看回放视频',
					success: function(res){
						if(res.confirm){
							console.log('确认')
						}else{
							console.log('取消')
						}
					}
				});
			},
			/**
			 * 获取已登录用户数据
			 */
			getUserInfo: function(){
				let that = this
				this.util.getUserInfo((res)=>{
					//console.log('userInfo',res)
					let userInfo = uni.getStorageSync('userInfo') || {}
					that.userInfo = userInfo
					that.getApiUserInfo()
					this.$emit('getData')
				})
			},
			
			/**
			 * 获取用户详情数据
			 */
			getApiUserInfo: function(){
				let that = this
				let data = {}
					data.action = 'user_info'
					data.m = 'muu_classroom'
				//动态获取用户数据
				let url = ''
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/ucenter'
					//#endif
					//#ifdef H5
					url = 'entry/site/ucenter'
					//#endif
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							that.userInfo.sessionid = that.userInfo.sessionid
							that.userInfo.memberInfo = res.data.data
							uni.setStorageSync('userInfo', that.userInfo)
							console.log(that.userInfo)
						}
						//结束下拉刷新
						uni.stopPullDownRefresh();
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	
	.history {
		background-color: #fff;
		padding: 20rpx;
		margin-bottom: 20rpx;
		.header {
			display: flex;
			.title {
				flex: 1;
				font-size: 28rpx;
				font-weight: 600;
				color: #000;
				margin-bottom: 16rpx;
			}
			.more {
				width: 160rpx;
				font-size: 24rpx;
				text-align: right;
				color: #999;
			}
		}
		
		.list {
			.item {
				padding: 0 0 10rpx 0;
				margin-bottom: 10rpx;
				.content {
					font-size: 28rpx;
					line-height: 48rpx;
					color: #888;
					.start-time {
						font-size: 28rpx;
						color: #000;
					}
					.desc {
						display: inline-block;
						font-size: 32rpx;
						margin-left: 20rpx;
					}
				}
				&:last-child {
					margin-bottom: 0;
					border-bottom: 0;
					padding-bottom: 0;
				}
			}
		}
	}
</style>
